package com.aspire.nm.component.miniServerDemo.clientTest;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.http.ParseException;

import com.aspire.nm.component.commonUtil.httpClient.HttpTookit;
import com.aspire.nm.component.miniServerDemo.Main;



public class ServerTest_lowEfficiency {

    public static void main(String [] args) throws InterruptedException, InstantiationException, IllegalAccessException{
        new ServerTest_lowEfficiency().test();
    }
    
    /**
     * for 
workQueueSize=500
maximumPoolSize=10
corePoolSize=5
     */
    
    private AtomicInteger SocketTimeoutCount=new AtomicInteger(0);
    private int c = 520;
    private CountDownLatch countDownLatch = new CountDownLatch(c);
    
    public void test() throws InterruptedException, InstantiationException, IllegalAccessException{
        
        for(int i = 0; i < c;i++){
            new Thread(){
                public void run(){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    HttpTookit httpTookit = null;
                    try {
                        httpTookit = new HttpTookit();
                        httpTookit.doGet("http://"+Main.serverIp4Test+":7070/demo/lowEfficiency", null);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }catch(java.net.SocketTimeoutException e){
                        SocketTimeoutCount.incrementAndGet();
                        try {
                            httpTookit.release();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }catch (IOException e) {
                        e.printStackTrace();
                    }catch(java.lang.RuntimeException e){
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                }
            }.start();
        }
        
        
        countDownLatch.await();
        
        System.out.println(SocketTimeoutCount.get() );
    }
    
    
    
}

